using System;
using UnityEngine;

namespace Function.SkillEditor.Runtime
{
    [Serializable]
    public class SkillAudioClip : SkillClip
    {
        [NonSerialized] private AudioSource _audioSource;

        public override void OnEnter()
        {
            SkillRunRender.Instance.SendMessage(_entityData._entityId, RenderState.Enter, this);
        }

        public override void OnExit()
        {
            SkillRunRender.Instance.SendMessage(_entityData._entityId, RenderState.Exit, this);
        }

        public override void Render(RenderState renderState, IClipRenderEnv renderEnvEnv)
        {
            if (renderState == RenderState.Enter)
            {
                _audioSource = AudioRender.PlayAudio(renderEnvEnv._go, _asset as AudioClip);
            }
            else if (renderState == RenderState.Exit)
            {
                if (_audioSource != null)
                {
                    AudioRender.StopAudio(renderEnvEnv._go, _audioSource);
                }
            }
        }
    }
}